**
**  This file corresponds to the paper:
**
**  Aidt, Toke, Zareh Asatryan, Lusine Badalyan and Friedrich Heinemann. 2018.
**        Vote Buying or (Political) Business (Cycles) as Usual?
**        The Review of Economics and Statistics, forthcoming.
**
**  This DO file replicates the second (micro) part of the analysis, see Chapter 4.4 (Consumption expenditures and elections: evidence from Armenia)
**  For the DO files replicating the first(macro) and third(micro-macro) parts of the analysis, see do_votebuying_macro.do and do_votebuying_micro-macro.do.
**
**  Date: 07 Dec 2018
**       

clear all
set more off, perm

** Change directory to local folder
cd "C:\Users\user\Desktop\Vote Buying\0_RESTAT"
* if not already ssc install the following programs: xtivreg2 outreg2 tabout xtabond2 egenmore coefplot


** Load data (household surveys from Armenia, 2001-16, including daily consumption diaries)
use consumption_hh_daily_arm.dta, clear 

** Create date of election and pseudo-election variables
generate DOE_1 = mdy(05, 12, 2007)
label var DOE_1 "Parliamentary elections in 2007"
format DOE_1 %tdMonth_dd,_CCYY
generate DOE_2 = mdy(05, 06, 2012)
label var DOE_2 "Parliamentary elections in 2012"
format DOE_2 %tdMonth_dd,_CCYY
generate DOE_3 = mdy(02, 19, 2003)
label var DOE_3 "1st round of Presidential elections in 2003"
format DOE_3 %tdMonth_dd,_CCYY
generate DOE_4 = mdy(02, 19, 2008)
label var DOE_4 "Presidential elections in 2008"
format DOE_4 %tdMonth_dd,_CCYY
generate DOE_5 = mdy(02, 18, 2013)
label var DOE_5 "Presidential elections in 2013"
format DOE_5 %tdMonth_dd,_CCYY

foreach num of numlist 2006 2008 {
 generate DOEpseudo`num' = mdy(05, 12, `num')
 label var DOEpseudo`num' "Pseudo-elections in `num'"
 format DOEpseudo`num' %tdMonth_dd,_CCYY
}
foreach num of numlist 2011 2013 {
 generate DOEpseudo`num' = mdy(05, 06, `num')
 label var DOEpseudo`num' "Pseudo-elections in `num'"
 format DOEpseudo`num' %tdMonth_dd,_CCYY
}
foreach num of numlist 2002 2004 {
 generate DOEpseudo`num' = mdy(02, 19, `num')
 label var DOEpseudo`num' "Pseudo-elections in `num'"
 format DOEpseudo`num' %tdMonth_dd,_CCYY
}
foreach num of numlist 2007 2009{
 generate DOEpseudo`num' = mdy(02, 19, `num')
 label var DOEpseudo`num' "Pseudo-elections in `num'"
 format DOEpseudo`num' %tdMonth_dd,_CCYY
}
foreach num of numlist 2012 2014{
 generate DOEpseudo`num' = mdy(02, 18, `num')
 label var DOEpseudo`num' "Pseudo-elections in `num'"
 format DOEpseudo`num' %tdMonth_dd,_CCYY
}

** Create treatment-control and after-before groups
cap drop after* treated*
forvalues i = 1(1)20 {  
scalar delta`i' = 2 * `i'
gen after`i' = .
gen treated`i' = .
foreach j of var DOE* {
replace after`i' = 1 if 0 <= ((`j'+14) - DOS) & ((`j'+14) - DOS) < delta`i'  
replace after`i' = 0 if delta`i' <= ((`j'+14) - DOS) & ((`j'+14) - DOS) < (2 * delta`i')
// assign 1 for pseudo and real elec. if DOS is in 0-delta window, 0 if in delta-2xdelta
}
foreach k of var DOEp* {
replace treated`i' = 0 if 0 <= ((`k'+14) - DOS) & ((`k'+14) - DOS) < (2 * delta`i')
// assign 0 (control) if DOS is in 0-2xdelta window of pseudo elections
}
foreach l of var DOE_* {
replace treated`i' = 1 if 0 <= ((`l'+14) - DOS) & ((`l'+14) - DOS) < (2 * delta`i')
// assign 1 (treatment) if DOS is in 0-2xdelta window of real elections
}
label var after`i' "=1 if HH was interviewed within delta days before any election"
label var treated`i' "=1 if HH was interviewed within 2xdelta days before real election"
}


**
**  Summary statistics of the sample (Table A.8) and consumption expenditure variables (Table 3)
**

preserve

// count number of observations by household
egen N_obs = count(DOS_day), by(recno_year)

foreach i of var pur_f* pur_nonfd_tobac totalpur_* x3_2 {
 local k: variable label `i'
 egen month_`i' = total(`i'), by(recno_year)
 replace month_`i' = month_`i' * (30 / N_obs)
 lab var month_`i' "Monthly expend `k'"
}

// distribution of observations for control-treatment and before-after 
tab treated20 after20

// count number of zero observations per food category
foreach j of var pur_food_* {
count if `j' == 0
local m_`j' = (r(N) / 2734856)
display `m_`j''
}

* Collapse the dataset (to go from daily to household -level data)
collapse (min) a1_2 DOS_month DOS_year (mean) marz hous* dum j1 no* major_ inc* total_in month* N_obs (median) treated20, by(recno_year)

* Number of households in the different groups and other statistics referenced in the paper
// how many households are in the control, how many in the treatment group
tab treated20

// how many households are in the sample for each year
tab DOS_year

// how many daily diary entries households submit on average
summarize N_obs 

* Ratios of food and nonfood consumption expenditures to income
// full sample
cap drop rat*
gen ratio_food_inc = month_totalpur_f/total_income
lab var ratio_food_inc "Ratio total food purchases to income"
gen ratio_nonfood_inc = month_totalpur_n/total_income
lab var ratio_nonfood_inc "Ratio total non-food purchases to income"
estpost summarize ratio_*, detail
est store A

// control group
drop ratio*
gen ratio_food_inc = month_totalpur_f/total_income if treated20==0
lab var ratio_food_inc "Ratio total food purchases to income (control)"
gen ratio_nonfood_inc = month_totalpur_n/total_income if treated20==0
lab var ratio_nonfood_inc "Ratio total non-food purchases to income (control)"
estpost summarize ratio_*, detail
est store B

// treatment group
drop ratio*
gen ratio_food_inc = month_totalpur_f/total_income if treated20==1
lab var ratio_food_inc "Ratio total food purchases to income (treatment)"
gen ratio_nonfood_inc = month_totalpur_n/total_income if treated20==1
lab var ratio_nonfood_inc "Ratio total non-food purchases to income (treatment)"
estpost summarize ratio_*, detail
est store C
esttab A B C using summary_stats_arm.txt, cells("p50(fmt(3)) p10(fmt(3)) p90(fmt(3))") label replace

* Ratios of food consumption categories to total food expenditures
// full sample
foreach j of var month_pur_f* {
 gen rat_`j' = `j'/month_totalpur_f
 local k: variable label `j'
 lab var rat_`j' "Ratio `k' to total food purchases"
}
estpost summarize rat_*, detail
est store A

// control group
drop rat_*
foreach j of var month_pur_f* {
 gen rat_`j' = `j'/month_totalpur_f if treated20==0
 local k: variable label `j'
 lab var rat_`j' "Ratio `k' to total food purchases (control)"
}
estpost summarize rat_*, detail
est store B

// treatment group
drop rat_*
foreach j of var month_pur_f* {
 gen rat_`j' = `j'/month_totalpur_f if treated20==1
 local k: variable label `j'
 lab var rat_`j' "Ratio `k' to total food purchases (treatment)"
}
estpost summarize rat_*, detail
est store C

esttab A B C using summary_stats_arm.txt, cells("p50(fmt(3)) p10(fmt(3)) p90(fmt(3))") label append

restore


**
**  Main results, average responses (Figure 5(a) and Figure 6)
**

* create interaction terms
cap drop treated_after*
forvalues i = 1(1)20 { 
generate treated_after`i' = treated`i' * after`i'
}

* to reduce calculation time drop all unaffected observations
drop if treated_after20 == .

cd graphs
xtset recno_year DOS

foreach j of var totalpur_f  pur_food* {   
local k: variable label `j'
sum `j', meanonly
local l = round(r(mean))
forvalues i = 18(1)18 { // set reference group variable
 rename treated_after`i' ATE
 xtreg `j' treated`i' after`i' ATE i.DOS_dayofyear i.DOS_weekday, fe vce(cluster se_clust)
 matrix myb = e(b)
 local adj = myb[1,3]
 rename ATE treated_after`i'
 }
forvalues i = 1(1)20 {
 rename treated_after`i' ATE
 xtreg `j' treated`i' after`i' ATE i.DOS_dayofyear i.DOS_weekday, fe vce(cluster se_clust)
 matrix myb = e(b)
 matrix myb[1,3] = myb[1,3]- `adj'
 erepost b = myb
 if `i' == 18 {
 matrix myv = e(V) 
 matrix myv[3,3] = 0
 erepost V = myv
 }
 estimates store coef`i'
 local m_`i': display %7.0fc e(N)
 rename ATE treated_after`i'
 }
 coefplot coef20 || coef19 || coef18 || coef17 || coef16 || coef15 || coef14 || coef13 || coef12 || coef11 || ///
	coef10 || coef9 || coef8 || coef7 || coef6 || coef5 || coef4 || coef3 || coef2, ///
	keep(ATE) yline(0) xline(14) vertical levels(95) ciopts(recast(rcap)) bycoefs ///
	xtitle("Window Size (in days)", size(small)) ///
	ytitle("Value in Armenian Dram", size(small)) ///
	xlabel(1 "40" 2 "38" 3 "36" 4 "34" 5 "32" 6 "30" 7 "28" 8 "26" 9 "24" 10 "22" 11 "20" 12 "18" 13 "16" 14 "14" 15 "12" 16 "10" 17 "8" 18 "6" 19 "4", ///
	labsize(small)) ylabel(, labsize(small)) note(Mean of `k': `l' Dram) 
graph export allelec_`j'_36adj.png, as(png) replace width(1000)
}	


**
**  Results by income quartile (Figure 5(b) and Figure A.1)
**

xtset recno_year DOS

//divide the sample into income quartiles by year to analyze heterogeneous effects
gen total_income_capita = total_income/no_mem
egen income_quart = xtile(total_income_capita), by(DOS_year) nq(4)

foreach j of var totalpur_f  pur_food*{ 
graph drop _all
local k: variable label `j'
sum `j', meanonly
local l = round(r(mean))
forvalues q = 1(1)4 {
forvalues i = 18(1)18 { // set reference group variable
 rename treated_after`i' ATE
 xtreg `j' treated`i' after`i' ATE i.DOS_dayofyear i.DOS_weekday if income_quart==`q', fe vce(cluster se_clust)
 matrix myb = e(b)
 local adj = myb[1,3]
 rename ATE treated_after`i'
 }
forvalues i = 1(1)20 {
 rename treated_after`i' ATE
 xtreg `j' treated`i' after`i' ATE i.DOS_dayofyear i.DOS_weekday if income_quart==`q', fe vce(cluster se_clust)
 matrix myb = e(b)
 matrix myb[1,3] = myb[1,3]- `adj'
 erepost b = myb
 if `i' == 18 {
 matrix myv = e(V) 
 matrix myv[3,3] = 0
 erepost V = myv
 }
 estimates store coef`i'
 local m_`i': display %7.0fc e(N)
 rename ATE treated_after`i'
 }
coefplot coef20 || coef19 || coef18 || coef17 || coef16 || coef15 || coef14 || coef13 || coef12 || coef11 || ///
	coef10 || coef9 || coef8 || coef7 || coef6 || coef5 || coef4 || coef3 || coef2, ///
	keep(ATE) yline(0) xline(14) vertical levels(95) ciopts(recast(rcap)) bycoefs ///
	title("Quartile `q'", size(medsmall)) xtitle("Window Size (in days)", size(small)) ///
	xlabel(1 "40" 2 "38" 3 "36" 4 "34" 5 "32" 6 "30" 7 "28" 8 "26" 9 "24" 10 "22" 11 "20" 12 "18" 13 "16" 14 "14" 15 "12" 16 "10" 17 "8" 18 "6" 19 "4", ///
	labsize(small)) ylabel(, labsize(small)) name(abc_`q')
}
graph combine abc_1 abc_2 abc_3 abc_4, ycom
graph export allelec_`j'_quartiles_36adj.png, as(png) replace width(1000)
}

clear
exit
